var util = require("../../utils/util.js")
// content.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    root:"",
    food:{},
    propertyvals:{},
    vals:{},
    properties:{},
    cartItem:{}  //购物车 的项  --单个菜 菜的id 和属性id 就ok了 还有数量 数量在列表页添加   foodId,propertyId,valId,num,
  },

  choicePropertyVal:function(event){
    var pid = event.target.dataset.pid;
    var vid = event.target.dataset.vid;
    var fid = this.data.food.id;
    this.setData({
      cartItem:{foodId:fid,propertyId:pid,valId:vid}
    });
  },

  doOrder:function(event){
    var cartItem = this.data.cartItem;
    cartItem.foodId = this.data.food.id;
    //存入全局变量
    var app = getApp();
    app.cart[cartItem.foodId] = cartItem;
    wx.showToast({
      title: '加入购物车成功',
      icon: 'success',
      duration: 2000,
      success:function(){
        wx.navigateBack();
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var foodId = options.id;
    var rootUrl= getApp().rootUrl;
    var that = this;
    that.setData({root:rootUrl});
    util.foxlogin({
      success:function(token){
        wx.request({
          url: rootUrl + "api/food",
          data:{id:foodId,token:token},
          success:function(responseData){
            //
            
            //整理属性和值的对应关系
            var propertyvals = {};
            var responsePropertyVals = responseData.data.propertyvals;
            for (var i = 0; i < responsePropertyVals.length;i++){
              if( typeof propertyvals[responsePropertyVals[i]['pid']] == "undefined" ){
                propertyvals[responsePropertyVals[i]['pid']] = [];
              }
              propertyvals[responsePropertyVals[i]['pid']].push(responsePropertyVals[i]);
            }
            console.log(propertyvals);
            //整理属性
            var properties = {}
            var responseProperties = responseData.data.properties;
            for(var i = 0;i<responseProperties.length;i++ ){
              properties[responseProperties[i]['id']] = responseProperties[i];  
            }

            //整理属性值名称
            var vals = {};
            var responseVals = responseData.data.vals;
            for(var i = 0; i< responseVals.length; i++ ){
              vals[responseVals[i]['id']] = responseVals[i];
            }
           

            that.setData({ food: responseData.data.food,propertyvals:propertyvals ,vals:vals,properties:properties});

          }
        })
      }
    });
    
    //初始化当前页购物车
    var cart = getApp().cart;
    var cartItem = cart[foodId]?cart[foodId]:{};
    this.setData({ cartItem: cartItem });
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (option) {
    console.log(option);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})